export const BASE_URL = 'http://127.0.0.1:8198/api'
import axios from "axios";
import { Message } from 'element-ui'
import router from '@/router'

axios.interceptors.request.use(
  config => {     
    return config;  
  },
  error => {
    return Promise.reject(error);
  }
)

axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
      if (error.response) {
          if (error.response.status === 401) {
            Message({
              type: 'error',
              message: error.response.data.msg
            })
            router.push({url: '/' })
              // 这种情况一般调到登录页
          } else if (error.response.status === 403) {
            Message({
              type: 'error',
              message: error.response.data.msg
            })
            router.push({url: '/' })
              // 提示无权限等
          } else {
              // 其他错误处理
              Message({
                type: 'error',
                message: error.response.data.msg
              })
              router.push({url: '/' })
          }
      }
      return Promise.reject(error.response.data)
  }
)

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.baseURL = BASE_URL
export const axios_instance = axios